package com.cloudkinto.controller.sc;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.vo.sc.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.sc.ScOverRuleService;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import java.util.HashMap;
import java.util.Map;
import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2024-11-05
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "超长超重omp模块")
@RequestMapping("/api/admin/omp/ScOverRule")
public class ScOverRuleController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ScOverRuleController.class);

    private final ScOverRuleService service;


    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated ScRuleAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功", service.add(req, userId, getTenantId()));
    }
    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated ScRuleUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId, getTenantId()));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ScRuleDetailRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }


    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "surchargeType", value = "类型"),
            @ApiImplicitParam(name = "surchargeName", value = "规则名称"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ScRulePageRes.class))
    public SingleResult list(HttpServletRequest request) {

        Map<String, String> map = this.getMap(request);

        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "surchargeType", value = "类型"),
            @ApiImplicitParam(name = "surchargeName", value = "规则名称"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ScRulePageRes.class))
    public PageResult listPage(HttpServletRequest request) {
        Map<String, String> map = this.getMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    public Map<String, String> getMap(HttpServletRequest request) {
        Long tenantId = getTenantId();

        Map<String, String> map = new HashMap<>();
        String surchargeName = request.getParameter("surchargeName");
        String surchargeType = request.getParameter("surchargeType");
        String status = request.getParameter("status");
        map.put("surchargeName", surchargeName);
        map.put("surchargeType", surchargeType);
        map.put("status", status);
        map.put("tenantId", tenantId+"");

        return map;
    }



    @UserSessionRequired
    @PutMapping("/trial")
    @ApiOperation(value = "测试规则", notes = "测试规则")
    public SingleResult trial(@RequestBody @Validated SurchargeTrialReq req) {
        return SingleResult.success(service.trial(req));
    }

}

